package com.lenovo.testreport.jenkins.service;

import com.lenovo.testreport.jenkins.JenkinsApi;
import com.lenovo.testreport.jenkins.model.Job;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * @author CJF
 * @date 2018/12/6
 */
@Service
public class jenkinsServiceImpl implements jenkinsService {

    @Autowired
    RedisTemplate<Object, Object> redisTemplate;
    RedisSerializer serializer = new StringRedisSerializer();

    @Override
    public List<Job> getList(String type) {
        redisTemplate.setKeySerializer(serializer);
        String key = "Jenkins/job/" + type.toLowerCase();
        List<Job> list = (List<Job>) redisTemplate.opsForValue().get(key);
        if (list == null) {
            try {
                list = JenkinsApi.getJobList(type);
                redisTemplate.opsForValue().set(key, list, 120, TimeUnit.SECONDS);
            } catch (IOException e) {
                e.printStackTrace();
                list = null;
            }
        }
        return list;

    }

    @Override
    public boolean buildJob(String jobName, Boolean isNeedParams, String buildJobParams) {
        int index = jobName.lastIndexOf('_');
        String type = jobName.substring(index + 1);
        String key = "Jenkins/job/" + type.toLowerCase();
        try {
            boolean result = JenkinsApi.buildJob(jobName, isNeedParams, buildJobParams);
            updateJobStatus(jobName, key);
            return result;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }


    @Override
    public boolean stopJob(String jobName, Boolean isNeedParams, String buildJobParams) {
        int index = jobName.lastIndexOf('_');
        String type = jobName.substring(index + 1);
        String key = "Jenkins/job/" + type.toLowerCase();
        try {
            boolean result = JenkinsApi.stopJob(jobName, isNeedParams, buildJobParams);
            updateJobStatus(jobName, key);
            return result;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }

    void updateJobStatus(String jobName, String key) {
        StringBuffer suffix = new StringBuffer("%");
        if (jobName.toLowerCase().contains("interface")) {
            suffix.append("interface");
        } else if (jobName.toLowerCase().contains("publiccloud")) {
            suffix.append("publiccloud");

        } else if (jobName.toLowerCase().contains("android")) {
            suffix.append("android");
        }
        redisTemplate.setKeySerializer(serializer);
        String key2 = key + suffix.toString();
        System.out.println(key2);
        ExecutorService executorService = Executors.newScheduledThreadPool(1);
        ((ScheduledExecutorService) executorService).schedule(() -> {
            Boolean delete = redisTemplate.delete(key2);
            System.out.println(delete);
        }, 10, TimeUnit.SECONDS);
        executorService.shutdown();
    }


}
